Home
C#.WinForms
Button Example
Updated Dec 24, 2024
Dot Net Perls
Button. In Windows Forms we use a Button control that accepts click events and performs other actions in the user interface. This control provides a way to accept input.
Add. In Visual Studio, go to the View menu and select the Toolbox option. Locate the Button item in the Toolbox window and either double-click it or drag it to your window.
Example. We see the body of the Click event that can be added to handle click events for the button1 control instance. You do not need to type in button1_Click.
Info You can use the interface in Visual Studio to create empty event handler methods.
Then You can insert logic such as calling the MessageBox.Show method call to perform a trivial action when the event is processed.
Next In the properties pane or window, you can either change the regular properties or the event handlers on the Button control.
MessageBox.Show
using System; using System.Windows.Forms; namespace WindowsFormsApplication21 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Dot Net Perls says hello.", "How is your day going?"); } } }
Click. Windows Forms programs are event-based. When you add a Click event handler to a Button, the event handler method will be invoked when the user clicks on the button.
private void button1_Click(object sender, EventArgs e) { // Created when the Click event handler is selected. }
Properties window. In this window, a lightning bolt signifies the event handlers list. You can click on it and then double-click on an event. The C# code will then be inserted.
Then You manually edit that C# code to add the program-specific logic. Any statements can be used.
And Instead creating a new event handler, you can use the drop-down box to select a method that already exists to handle the events.
Info The method signature must be compatible—wiring multiple events to one event handler is useful.
Discussion. By specifying the constraints of your Button, you can avoid modifying pixel dimensions. You should experiment with the Anchor property to control the resizing of the Button.
Further I recommend putting Buttons inside TableLayoutPanel cells, as this reduces the need to position them manually.
Also Consider setting the Enable property's value to an expression based on another part of the user interface.
Summary. We explored the Button control in the Windows Forms control set. We added add a new instance of the Button control. We next changed the appearance of the Button control and its text.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 24, 2024 (new example).
Home
Changes
© 2007-2025 Sam Allen